package com.ericzong.renamer.converter.string;

import org.apache.commons.lang3.StringUtils;

import com.ericzong.renamer.model.Record;

public class NumberStringConverter extends AbstractStringConverter
{
	private String expression;
	private int startNumber;
	private int interval;
	private int digitNumber;
	
	private int currentNumber;

	public NumberStringConverter()
	{
		expression = StringUtils.EMPTY;
		startNumber = 1;
		interval = 1;
		digitNumber = 1;
		reset();
	}

	@Override
	public void convert(Record record)
	{
		String target = record.getSource();
		
		if(StringUtils.isNoneEmpty(expression))
		{
			target = expression.replace("*", target);
			target = target.replace("#", getNumber());
		}
		
		record.setTarget(target);
	}
	
	private String getNumber()
	{
		String number = String.format(getFormat(), currentNumber).replace(" ", "0");
		currentNumber += interval;
		return number;
	}
	
	private String getFormat()
	{
		return "%" + digitNumber + "d";
	}
	
	public final void reset()
	{
		currentNumber = startNumber;
	}

	public String getExpression()
	{
		return expression;
	}

	public void setExpression(String expression)
	{
		this.expression = expression;
	}

	public int getStartNumber()
	{
		return startNumber;
	}

	public void setStartNumber(int startNumber)
	{
		this.startNumber = startNumber;
		reset();
	}

	public int getInterval()
	{
		return interval;
	}

	public void setInterval(int interval)
	{
		this.interval = interval;
	}

	public int getDigitNumber()
	{
		return digitNumber;
	}

	public void setDigitNumber(int digitNumber)
	{
		this.digitNumber = digitNumber;
	}

}
